package my.p2p.client.util;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author fengbo
 */
public class ThreadUtils {
    private static ExecutorService executorService = new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS, new SynchronousQueue<>(),
            new DefaultThreadFactory("sync-store-order-wait-"));

    private static class DefaultThreadFactory implements ThreadFactory {

        private final String namePrefix;
        private AtomicInteger idx = new AtomicInteger();

        DefaultThreadFactory(String namePrefix) {
            this.namePrefix = namePrefix;
        }

        @Override
        public Thread newThread(Runnable task) {
            Thread thread = Executors.defaultThreadFactory().newThread(task);
            thread.setName(namePrefix + idx.getAndIncrement());
            return thread;
        }
    }

    public static void execute(Runnable task) {
        executorService.execute(task);
    }
}
